package com.xgkj.eatshow.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.util.PalLog;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "WXPayEntryActivity";
	public static final String WEIXIN_PAY_SUCCESS_ACTION = "weixin_pay_success_action";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, ProtocolConst.APP_ID);

        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG,"onPayFinish, errCode = " + resp.errCode);
		Intent intent = new Intent(WEIXIN_PAY_SUCCESS_ACTION);
		PayResp payResp = (PayResp) resp;
		PalLog.e(TAG, payResp.returnKey + "****" + payResp.extData + "****" + payResp.prepayId + "******" + payResp);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if(resp.errCode == 0){
				intent.putExtra(ProtocolConst.WEIXINPAY_SUCCESS, true);
			}else{
				intent.putExtra(ProtocolConst.WEIXINPAY_SUCCESS, false);
			}
			
			sendBroadcast(intent);
			finish();
		}
	}
}